/**
 * Created by Administrator on 2017/9/19.
 */

/**
 * Created by Administrator on 2017/9/21.
 */
requirejs.config({
	baseUrl:"../js",
	paths:{
		"jquery":"lib/jquery-3.2.1",
		"cookie":"lib/jquery.cookie",
		"fly":"lib/jquery.fly.min",
		"lazyload":"lib/jquery.lazyload"
	},
	shim:{
		"cookie":{
			deps:["jquery"],
			exports:"cookie"
		},
		"fly":{
			deps:["jquery"],
			exports:"fly"
		},
		"lazyload":{
			deps:["jquery"],
			exports:"lazyload"
		},
	}
});
requirejs(["jquery","cookie","fly","lazyload"],function ($) {
	
	$(function () {
		
		//查看是否是用户
		var User = window.location.search;
		if(User .indexOf("uphone")!=-1){
			var arr = User .split("=");
			User  = arr[1];
			
			var oLi = $(".head_top_left").children("ul").children("li");
			oLi.eq(1).html("欢迎您，知我用户:"+arr[1]);
			oLi.eq(2).children("a").text("[退出]");
			oLi.eq(2).children("a").on("click",function () {
				$(window).attr("location","../html/login.html");
			})
		}else{
			var oLi = $(".head_top_left").children("ul").children("li");
			User  = "merchandise";
			oLi.eq(2).children("a").on("click",function () {
				$(window).attr("location","../html/registration.html");
			})
		};
		
		//header_topnav划过有下拉门效果
		$(".hover_bg").on("mouseenter",function () {
			$(this).children("div").stop().slideDown();
		}).on("mouseleave",function () {
			$(this).children("div").stop().slideUp();
		});
		
		//head_cshop划过有下拉效果,
		$(".head_cshop").on("mouseenter",function () {
			//显示购物车东西
			shoppingCart();
			$(this).children("a").find("i").css("background-position","0 -105px");
			$(this).children("a").addClass("change").end().children(".shop_goods").stop().slideDown();
		}).on("mouseleave",function () {
			$(this).children("a").find("i").css("background-position","0 -70px")
			$(this).children("a").removeClass("change").end().children(".shop_goods").stop().slideUp();
		})
		
		//nav2划过有下拉效果
		$(".nav2").on("mouseenter",function () {
			$(this).children(".mnav_nav2").stop().slideDown(500);
		}).on("mouseleave",function () {
			$(this).children(".mnav_nav2").stop().slideUp(500);
		})
		
		//滚动条上方靠不见的距离大于主体距上面的距离，左侧栏出现
		$(window).on("scroll",function (evt) {
			var $height = $("#header").height()+$("#banner").height();
			if(scrollY>$height){
				$("#left_side").stop().fadeIn(300)
			}else{
				$("#left_side").stop().fadeOut(300)
			}
		});
		//划过左侧栏效果
		$("#left_side>ul>li").on("mouseover",function () {
			$(this).css("background","#00c8ff").children("a").css("color","#fff")
		}).on("mouseleave",function () {
			$(this).css("background","#fff").children("a").css("color","rgb(108, 108, 108)")
		});
		
		
		//右侧栏的划过效果
		var oLi = $(".side_menu>ul>li");
		//鼠标移入变色和模块的出现
		oLi.on("mouseover",function () {
			$(this).css("background","#00c8ff").children(".animat_div").stop().show().animate({left:-103,opacity:1}).end().children(".clear").show();
		}).on("mouseout",function () {
			$(this).css("background","").children(".animat_div").stop().animate({left:-130,opacity:0}).end().children(".clear").hide().end().parent().find(".animat_div").hide();
		});
		//点击购物车出现购物车筐
		$(".menu_2").on("click",function (event) {
			//显示购物车东西
			shoppingCart();
			event.stopPropagation();//阻止冒泡事件
			$(".side_content").animate({left:-300})
		});
		//点击尖括号购物车框框回去
		$(".side_content").find(".back").on("click",function () {
			$(".side_content").animate({left:0});
		});
		// $("body").on("click",function () {
		// 	$(".side_content").animate({left:0})
		// })
		
		//点击top置顶
		$(".menu_7").on("click",function () {
			$(window).scrollTop(0);
		});
		
		
		
		$("#banner>ul>li").eq(0).css({zIndex:3,opacity:1}).children("a").animate({height:350},2000,"linear").end().siblings("li").css({"z-index":0,"opacity":1}).children("a").height(380);
		//轮播图
		var time = setInterval(autoPlay,5000);
		var num = 0;
		//鼠标移入停止
		$("#banner").on("mouseover",function () {
			clearInterval(time);
		}).on("mouseleave",function () {
			clearInterval(time);
			time = setInterval(autoPlay,5000)
		})
		//点击按钮
		$("#enter").children("span").on("click",function () {
			num = $(this).index();
			$(this).addClass("_color").siblings("span").removeClass("_color");
			$("#banner>ul>li").eq(num).css({zIndex:3,opacity:1}).children("a").animate({height:350},2000,"linear").end().siblings("li").css({"z-index":0,"opacity":1}).children("a").height(380);
		})
		
		//用户登陆后显示名字
		// var str = window.location.search;
		// var oLi = $(".head_top_left").children("ul").children("li");
		// if(str.indexOf("uphone")!=-1){
		// 	var arr = str.split("=");
		// 	var oLi = $(".head_top_left").children("ul").children("li");
		// 	oLi.eq(1).html("欢迎您，知我用户:"+arr[1]);
		// 	oLi.eq(2).children("a").text("[退出]");
		// 	oLi.eq(2).children("a").on("click",function () {
		// 		$(window).attr("location","../html/login.html");
		// 	})
		// }else{
		// 	oLi.eq(2).children("a").on("click",function () {
		// 		$(window).attr("location","../html/registration.html");
		// 	})
		// };
		
		//倒计时ct_rig_1
		var now = new Date();
		var hour = 24;
		var min = now.getMinutes()+21;
		var sec = now.getSeconds()+30;
		var future = now;
		future.setHours(hour);
		future.setMinutes(min);
		future.setSeconds(sec);
		var tmp = setInterval(countDown,1000);
		
		//划过图片有阴影ctop_left
		$(".ctop_left").on("mouseover",function () {
			$(this).find("img").animate({opacity:0.9},100);
		}).on("mouseleave",function () {
			$(this).find("img").animate({opacity:1},10);
		});
		
		//中间部分的nav   cnav_top
		$(".cnav_top>ul>li").on("mouseover",function () {
			$(this).addClass("show_nav").siblings("li").removeClass("show_nav");
			$(".main_1").eq($(this).index()).show().siblings("div").hide();
		});
		//main_right中的箭头切换
		$(".main_right>span").on("click",function () {
			var $index = $(this).parent().parent().index();
			if($index!=0){
				$(".cnav_top>ul>li").eq($index-1).addClass("show_nav").siblings("li").removeClass("show_nav");
				$(".main_1").eq($index-1).show().siblings("div").hide();
			}
		});
		$(".main_right>i").on("click",function () {
			var $index = $(this).parent().parent().index();
			if($index!=3){
				$(".cnav_top>ul>li").eq($index+1).addClass("show_nav").siblings("li").removeClass("show_nav");
				$(".main_1").eq($index+1).show().siblings("div").hide();
			}
		})
		
		//划过商品列表有透明goods_img
		$(".goods_img").find("img").on("mouseover",function () {
			$(this).siblings("#progress").hide();
			$(this).css("opacity",0.6);
		}).on("mouseleave",function () {
			$(this).siblings("#progress").show();
			$(this).css("opacity",1)
		});
		
		
		
		//上传json文件
		$.ajax({
			url:"../js/json/details.json",
			type:"get",
			dataType:"json",
			success:function (res) {
				$.each(res,function (index,obj) {
					if(index>3){
						if(index==4){
							var newLi = $(".n_list>ul>li").eq(0)
						}else{
							var newLi = $(".n_list>ul>li").eq(0).clone(true);
						}
						// newLi.find("img").attr("src", obj.src);
						newLi.find("img").attr("data-original",obj.src);
						newLi.find(".goods_name").find("b").text(obj.name);
						newLi.find(".goods_name").find("span").text(obj.author);
						newLi.find(".goods_name").find("i").text(obj.others);
						newLi.find(".goods_price").find("span").text(obj.price);
						newLi.find(".goods_sale>p").find("span").text(obj.buyNum);
						newLi.find(".goods_price>p>a")[0].index = obj.id;
						$(".n_list>ul").append(newLi);
						newLi.find("img").lazyload({
							effect : "fadeIn" ,//淡淡的出现，渐渐出现
						});
					}else{
						//在ctop_top加入
						if(index==0){
							var newLi = $(".con_top li").eq(0);
						}else{
							var newLi = $(".con_top li").eq(0).clone(true);
						}
						newLi.find("img").attr("src", obj.src);
						newLi.find(".ct_rig_2>a").text(obj.name);
						newLi.find(".ct_rig_3 span").text(obj.price);
						newLi.find(".ct_rig_4>div").text(obj.others);
						newLi.find(".ct_rig_3>p>a")[0].index = obj.id;
						$(".con_top").find("ul").append(newLi);
					}
				})
			}
		});
		//tab栏的图片
		$.ajax({
			url:"../js/json/brands.json",
			type:"get",
			dataType:"json",
			success:function (res) {
				$.each(res,function (k,arr) {
					var newDiv = $(".clone").eq(0).clone(true);
					newDiv.css("display","none");
					$.each(arr,function (index,val) {
						if(index!=10){
							newDiv.find(".main_left").find("img").eq(index).attr("src",val);
						}else{
							newDiv.find(".main_right").find("img").attr("src",val);
						}
					});
					newDiv.appendTo($(".cnav_center"))
				})
			}
		});
		
		
		//加入购物车,存入cookie
		//cookie中的物品
		$(".ct_rig_3>p>a").on("click",function (event) {
			var arr = $.cookie(User);
			var obj = {id:this.index};
			var self = this;
			traverseCookie(self,arr,obj);
			carryCookie();
			//引用插件
			var $imgSrc = $(this).parents(".ctop_right").prev(".ctop_left").find("img").attr("src");
			var $flyer = $('<img class="u-flyer" src="'+$imgSrc+'">');
			var _left = event.screenX;
			var _top = event.screenY-$(this).height()*2
			//飞
			$flyer.fly({
				//起点
				start: {left: _left, top: _top},
				//终点
				end: {left: 1320, top: 200, width: 0, height: 0},
				//到达终点后调用的
				onEnd: function(){
					console.log("到达终点");
					$flyer.remove();
				}
			});
		})
		// 商品列表中的cookie存放
		$(".goods_price>p>a").on("click",function () {
			var arr = $.cookie(User);
			var obj = {id:this.index};
			var self = this;
			traverseCookie(self,arr,obj);
			carryCookie();
			
			//引用插件
			var $imgSrc = $(this).parents(".goods_info").prev(".goods_img").find("img").attr("src");
			var $flyer = $('<img class="u-flyer" src="'+$imgSrc+'">');
			var _left = event.screenX;
			var _top = event.screenY-$(this).height()*2
			//飞
			$flyer.fly({
				//起点
				start: {left: _left, top: _top},
				//终点
				end: {left: 1320, top: 200, width: 0, height: 0},
				//到达终点后调用的
				onEnd: function(){
					console.log("到达终点");
					$flyer.remove();
				}
			});
		});
		
		
		//跳转到购物车的操作;
		$(".order").on("click",function () {
			if(User=="merchandise"){
				$(window).attr("location","../html/login.html");
			}else{
				$(window).attr("location","../html/shopping.html?uphone="+User);
			}
		});
		
		//跳转到商品详情页面
		$(".n_list #particulaers2").find(".goods_img>a").on("click",function () {
			var id = $(this).parents(".goods_img").siblings(".goods_info").find(".goods_price>p>a")[0].index;
			$(window).attr("location","../html/particulars.html?uphone="+User+"&id="+id);
		})
		$(".ctop_left").on("click",function () {
			var id = $(this).siblings(".ctop_right").find(".ct_rig_3 a")[0].index;
			$(window).attr("location","../html/particulars.html?uphone="+User+"&id="+id);
		});
		$(".ct_rig_2").on("click",function () {
			var id = $(this).siblings(".ct_rig_3").find("a")[0].index;
			$(window).attr("location","../html/particulars.html?uphone="+User+"&id="+id);
		})
		
		
		//减号--购物车
		$("#down_num").on("click",function () {
			var $num = $(this).siblings("#_num").val() - 1;
			if($num>=1){
				var self = $(this).parents(".info_name").find("div>a")[0];
				var arr = $.cookie(User);
				var obj = {id:self.index};
				//DOM改变
				$(this).siblings("#_num").val($num);//改变数量DOM操作
				var mon = $(this).parent().siblings("span").find("i").text()-0;
				var allmon = $(this).parents(".goods_msg").siblings(".goods_all").find("div>p>i");
				console.log(mon);
				allmon.text((allmon.text()-0-mon).toFixed(2));
				//end DOM改变
				traverseCookie(self,arr,obj,-1);
				carryCookie();
			}
		});
		//加号--购物车
		$("#up_num").on("click",function () {
			var $num = $(this).siblings("#_num").val() -0  + 1;
			var self = $(this).parents(".info_name").find("div>a")[0];
			var arr = $.cookie(User);
			var obj = {id:self.index};
			//DOM改变
			$(this).siblings("#_num").val($num);//改变数量DOM操作
			var mon = $(this).parent().siblings("span").find("i").text()-0;
			var allmon = $(this).parents(".goods_msg").siblings(".goods_all").find("div>p>i");
			console.log(mon);
			allmon.text((allmon.text()-0+mon).toFixed(2));
			//end DOM改变
			traverseCookie(self,arr,obj,1);
			carryCookie();
		})
		//删除键--购物车
		$(".info_name").find("div>a").on("click",function () {
			var arr = $.cookie(User);
			deleteCookie(this,arr);
		});
		
		
		/*
		函数
		 */
		//轮播自动播放
		function autoPlay() {
			num++;
			if(num==5){
				num = 0;
			};
			$("#banner>div>span").eq(num).addClass("_color").siblings("span").removeClass("_color");
			$("#banner>ul>li").eq(num).css({zIndex:3,opacity:1}).children("a").animate({height:350},2000,"linear").end().siblings("li").css({"z-index":0,"opacity":1}).children("a").height(380);
		}
		//倒计时
		function countDown() {
			now = new Date();
			var a = Date.parse(future);
			var b = Date.parse(now);
			var maxD = Math.max(a,b);
			var minD = Math.min(a,b);
			if(future<now){
				$(".ct_rig_1").children("i").text("团购结束").siblings("span").text();
				clearInterval(tmp);
			}
			var date = maxD - minD;
			var ss = parseInt(date/1000)%60;
			var mm = parseInt(date/1000/60)%60;
			var hh = parseInt(date/1000/60/60)%24;
			var dd = parseInt(date/1000/60/60/24);
			var dd2 = dd+2;
			var hh2 = hh-1;
			if(String(ss).length==1){ss="0"+ss};
			if(String(mm).length==1){mm="0"+mm};
			if(String(hh).length==1){hh="0"+hh};
			if(String(hh2).length==1){hh2="0"+hh2};
			
			var str = dd+'天'+hh+'时'+mm+'分'+ss+'秒';
			$(".ct_rig_1").children("span").text(str);
			
			var str2 = dd2+'天'+hh2+'时'+mm+'分'+ss+'秒';
			$(".goods_sale>div>span").text(str2);
		}
		//购物车中是否显示内容,以及cookie中的物品存入
		// shoppingCart();
		function shoppingCart() {
			// 清空原来存在的
			// $(".head_cshop .goods_msg>ul").children("li~li").remove();
			$(".content_goods .goods_msg>ul").children("li").remove();
			$(".head_cshop .goods_msg>ul").children("li").eq(0).siblings("li").remove();
			//显示东西
			carryCookie();
			var shopGoods = $.cookie(User);
			if(shopGoods!=undefined){
				$(".goods_has").show().siblings(".goods_empty").hide();
				var goods = JSON.parse(shopGoods);
				var count = 0;
				$.ajax({
					url:"../js/json/details.json",
					type:"get",
					success:function (res) {
						var allMoney = 0;
						$.each(res,function (index,obj) {
							$.each(goods,function (key,val) {
								if(obj.id==val.id){
									count++;
									if(count==1){
										var newLi = $(".head_cshop .goods_msg>ul>li").eq(0);
									}else{
										var newLi = $(".head_cshop .goods_msg>ul>li").eq(0).clone(true);
									}
									newLi.children("a").find("img").attr("src",obj.src);
									newLi.children(".info_name").children("a").text(obj.name);
									newLi.children(".info_name").find("div>span>i").text(obj.price);
									newLi.find("#_num").val(val.num);
									newLi.children(".info_name").find("div>a")[0].index = val.id;
									var newLi2 = newLi.clone(true);
									newLi2.children(".info_name").find("div>a")[0].index = val.id;
									$(".head_cshop .goods_msg>ul").append(newLi);
									$(".content_goods .goods_msg>ul").append(newLi2);
									allMoney = allMoney+((obj.price*val.num).toFixed(2)-0);
								}
							})
						});
						$(".goods_all>div>p>i").text(allMoney);
					}
				});
				if(goods.length==0){
					$.removeCookie(User);
					$(".goods_empty").show().siblings(".goods_has").hide();
					$(".list_empty").show().siblings("div").hide();
					$(".main_top").hide();
					carryCookie();
				}
			}else{
				$(".goods_empty").show().siblings(".goods_has").hide()
			}
		}
		//购物车显示商品个数
		carryCookie();
		function carryCookie() {
			var num = 0;
			if($.cookie(User)!=undefined){
				var arrJson = JSON.parse($.cookie(User));
				for(var i=0;i<arrJson.length;i++){
					num += arrJson[i].num;
				}
				$(".head_cshop>p").show().text(num);
				$(".menu_2>a>b").text(num);
			}else{
				$(".head_cshop>p").hide().text(1);
				$(".menu_2>a>b").text(0);
			};
			//显示个数
			$(".goods_all>p>span").text(num);
		};
		//cookie遍历
		function traverseCookie(self,arr,obj,number)  {
			if(number==undefined){
				number = 1;
			}
			var newArr = [];
			if(arr!=undefined){
				newArr = JSON.parse(arr);
			}
			for(var i=0;i<newArr.length;i++){
				if(self.index==newArr[i].id){
					newArr[i].num += number;
					// if(newArr[i].num<=0){
					// 	newArr.splice(i,1);
					// 	$.cookie(User,JSON.stringify(newArr),{expires:100});
					// 	shoppingCart();
					// 	return;
					// }
					$.cookie(User,JSON.stringify(newArr),{expires:100});
					return;
				}
			}
			obj.num = number;
			newArr.push(obj);
			$.cookie(User,JSON.stringify(newArr),{expires:100});
			arr = $.cookie(User);
			console.log(JSON.parse(arr));
			console.log(newArr);
		};
		//删除cookie中的对应的id
		function deleteCookie(self,arr) {
			var newArr = [];
			if(arr!=undefined){
				newArr = JSON.parse(arr);
				for(var i=0;i<newArr.length;i++){
					if(self.index==newArr[i].id){
						newArr.splice(i,1);
						$.cookie(User,JSON.stringify(newArr),{expires:100});
						carryCookie();
						shoppingCart();
						return;
					}
				}
			}
		}
	})
})